<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Controller\user;

use App\Job\OrderCouponTimeOutJob;

use App\Dao\UserCouponDao;
use App\Kernel\Util\UserJwtInstance;
use App\Request\User\OrderCoupon\CouponBuyRequest;
use App\Middleware\User\UserMiddleware;

use App\Request\User\OrderCoupon\PayRequest;
use App\Service\User\CouponPaymentService;
use App\Service\User\OrderCouponService;
use App\Service\User\PingPpService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * 购买优惠券
 *
 * @AutoController()
 * @Middleware(UserMiddleware::class)
 * @author 张昊(2396675557@qq.com)
 * @package App\Controller\admin
 */
class OrderCouponController extends Controller
{
    /**
     * @Inject
     * @var OrderCouponService
     */
    private $service;

    /**
     * @Inject
     * @var CouponPaymentService
     */
    private $coupon_payment;

    /**
     * @Inject
     *
     * @var PingPpService
     */
    private $ping_service;

    /**
     * 优惠券下单
     * @param CouponBuyRequest $request
     */
    public function buy(CouponBuyRequest $request)
    {
        $params = $request->all();

        $params['safe_pwd'] = isset($params['safe_pwd']) ? $params['safe_pwd'] : $this->error();

        $user = UserJwtInstance::instance()->build()->getUser();

        if( !empty($user->safe_pwd) ) {
            //验证安全密码
            if( !password_verify( $params['safe_pwd'], $user->safe_pwd ) ) {
                $this->error('ORDER.REGISTER_FAIL');
            }
        } else {
            $this->error('ORDER.REGISTER_EMPTY');
        }

        $result = $this->service->buy($params);

        // 异步队列，超时未支付关闭订单
        $this->driver->push(new OrderCouponTimeOutJob($result['order_no']), 30*60);

        $result ? $this->success( $result ) : $this->error();

    }

    /**
     * 未支付优惠券订单重新支付
     */
    public function unpaid()
    {
        $order_id = (int)$this->request->input('order_id', 0);
        $safe_pwd = (string)$this->request->input('safe_pwd', '');
        $channel = (string)$this->request->input('channel', '');
        if ($order_id === 0) $this->error();

        $user = UserJwtInstance::instance()->build()->getUser();
        if( !empty($user->safe_pwd) ) {
            //验证安全密码
            if( !password_verify( $safe_pwd, $user->safe_pwd ) ) {
                $this->error('ORDER.REGISTER_FAIL');
            }
        } else {
            $this->error('ORDER.REGISTER_EMPTY');
        }

        $result = $this->service->unpaid($order_id,$channel);

        $this->success($result);
    }

    /**
     * 优惠券支付
     *
     * @param PayRequest $request
     */
    public function pay(PayRequest $request)
    {
        $order_no = (string)$request->input('order_no');
        $pay_type = (string)$request->input('pay_type');

        $result = $this->service->pay( $order_no, $pay_type );

        $payment = $this->ping_service->coupon_pay($result, $pay_type,  $result['title'], '优惠券支付');
        //$payment = $this->coupon_payment->buildPay($result, $pay_type, '优惠券支付');

        $this->success($payment);
    }
}